package com.irain.pp.account.repository.enums;

import com.irain.pp.account.repository.model.PpUserDO;
import com.irain.pp.tool.ValidatorUtil;

import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @author iRain
 * @date 2017/12/6 9:27
 */
public enum AccountType {
    //用户名称
    USER_NAME(PpUserDO::getUserName, PpUserDO::setUserName, true),
    //手机号
    MOBILE(PpUserDO::getMobile, PpUserDO::setMobile, true),
    //邮箱
    EMAIL(PpUserDO::getEmail, PpUserDO::setEmail, true),
    //WEIXIN,
    //QQ,
    ;

    private Function<PpUserDO, String> getFunc;
    private BiConsumer<PpUserDO, String> setFunc;
    private boolean allowPasswordLogin;

    AccountType(Function<PpUserDO, String> getFunc, BiConsumer<PpUserDO, String> setFunc, boolean allowPasswordLogin) {
        this.getFunc = getFunc;
        this.setFunc = setFunc;
        this.allowPasswordLogin = allowPasswordLogin;
    }

    public String getFiled(PpUserDO ppUserDO) {
        return getFunc.apply(ppUserDO);
    }

    public void setFiled(PpUserDO ppUserDO, String filedValue) {
        setFunc.accept(ppUserDO, filedValue);
    }

    public boolean isAllowPasswordLogin(){
        return allowPasswordLogin;
    }

    public static AccountType parseLoginName(String loginName){
        if (ValidatorUtil.isMobile(loginName)){
            return MOBILE;
        } else if (ValidatorUtil.isEmail(loginName)){
            return EMAIL;
        } else if (ValidatorUtil.isUsername(loginName)){
            return USER_NAME;
        } else {
            throw new IllegalArgumentException();
        }
    }
}
